#ifndef DATABASE_HPP
#define DATABASE_HPP

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <set>

using namespace std; // 打开输出文件

ofstream outfile("output.txt");

class field
{
public:
    string name, content;
    int len;
    field()
    {
        name = "0";
        len = 0;
        content = "0";
    }
    void set_content(string _content)
    {
        if (_content.size() == len)
            content = _content;
        else
        {
            // 长度不足补0
            string stmp;
            for (int i = 0; i < len; i++)
            {
                if (i < _content.size())
                    stmp += _content[i];
                else
                    stmp += '0';
            }
            content = stmp;
        }
    }

    field(const field &f)
    {
        name = f.name;
        len = f.len;
        content = f.content;
    }
    void set(string _name, int _len, string _content)
    {
        name = _name;
        len = _len;
        set_content(_content);
    }
    field &operator=(const field &f)
    {
        this->name = f.name;
        this->len = f.len;
        this->content = f.content;
        return *this;
    }
    bool operator<(const field &f) const
    {
        if (name != f.name)
            return name < f.name;
        else
            return content < f.content;
    }

    bool operator==(const field &f)  const
    {
        if (name != f.name)
            return false;
        if (content != f.content)
            return false;
        return true;
    }

    void output() const
    {
        cout << "<" << name << ":" << len << ">" << content;
    }
    void output1() const
    {
        cout << name << ":" << content << ",";
    }
};

class record // record中不存在相同的field
{
public:
    set<field> R;
    field qso, tim, cal;
    record()
    {
        qso.set("QSO_DATE", 8, "0");
        tim.set("TIME_ON", 6, "0");
    }
    void import(vector<field> v)
    {
        for (int i = 0; i < v.size(); i++)
        {
            if (qso.name == v[i].name)
                qso.set_content(v[i].content);
            else if (tim.name == v[i].name)
                tim.set_content(v[i].content);
            else
            {
                if (v[i].name == "CALL")
                {
                    cal = v[i];
                }
                R.insert(v[i]);
            }
        }
    }
    bool operator==(const record &r) const
    {
        if (qso.content != r.qso.content)
            return false;
        if (tim.content != r.tim.content)
            return false;
        return R == r.R;
    }
    bool operator<(const record &r) const
    {
        if (qso < r.qso)
            return true;
        else if (qso.content == r.qso.content)
            return tim.content < r.tim.content;
        else
            return false;
    }
    bool operator<=(const record &r) const
    {
        if (qso < r.qso)
            return true;
        else if (qso.content == r.qso.content)
        {
            if (tim.content == r.tim.content)
                return true;
            else if (tim.content < r.tim.content)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    void output() const
    {
        qso.output();
        tim.output();
        for (auto it = R.begin(); it != R.end(); it++)
        {
            (*it).output();
        }
        cout << endl;
    }
    void output1() const
    {
        qso.output1();
        tim.output1();
        for (auto it = R.begin(); it != R.end(); it++)
        {
            (*it).output1();
        }
        cout << endl;
    }
};

class database
{
public:
    set<record> D;
    database() {}
    void import(vector<record> v)
    {
        for (int i = 0; i < v.size(); i++)
        {
            D.insert(v[i]);
        }
    }
    void output()
    {
        for (auto it = D.begin(); it != D.end(); it++)
        {
            (*it).output();
        }
    }

    void del(vector<record> v)
    {
        for (int i = 0; i < v.size(); i++)
        {
            auto iter = D.find(v[i]);
            if (iter != D.end())
            {
                D.erase(v[i]);
            }
        }
    }

    void outputcsv()
    {
        record rtmp = *D.begin();
        cout << rtmp.qso.name << "," << rtmp.tim.name;
        for (auto iter = rtmp.R.begin(); iter != rtmp.R.end(); iter++)
        {
            field tmp = *iter;
            cout << "," << tmp.name;
        }
        cout << endl;
        for (auto it = D.begin(); it != D.end(); it++)
        {

            cout << (*it).qso.content << "," << (*it).tim.content;
            for (auto iter = (*it).R.begin(); iter != (*it).R.end(); iter++)
            {
                field tmp = *iter;
                cout << "," << tmp.content;
            }
            cout << endl;
        }
    }
};

#endif